package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

func GetTeacherInfo(ctx *gin.Context) {
	id := ctx.Param("id")
	teacher, _ := eduTeacherService.SelectByID(id)
	courses, _ := eduCourseService.SelectCourseByTeacherID(id)
	ctx.JSON(http.StatusOK, gin.H{
		"code":       20000,
		"teacher":    teacher,
		"courseList": courses,
	})
	return
}

func GetTeacherInfoList(ctx *gin.Context) {
	page := ctx.Param("page")
	pageSize := ctx.Param("pageSize")

	p, _ := strconv.Atoi(page)
	ps, _ := strconv.Atoi(pageSize)
	teachers, _ := eduTeacherService.SelectFrontTeacherInfoList(p, ps)
	count, _ := eduTeacherService.SelectAllCount()

	// 判断是否有前一页
	hasPrevious := false
	if page != "1" {
		hasPrevious = true
	}

	// 判断是否有后一页
	hasNext := true
	t := count / int64(ps)
	if int64(p)-1 == t {
		hasNext = false
	}

	// 计算所有的页数
	pages := make([]int, 0)
	for i := 0; i <= int(t); i++ {
		pages = append(pages, i+1)
	}

	dataMap := make(map[string]interface{})
	dataMap["total"] = count
	dataMap["records"] = teachers
	dataMap["hasPrevious"] = hasPrevious
	dataMap["current"] = p
	dataMap["hasNext"] = hasNext
	dataMap["pages"] = pages

	ctx.JSON(http.StatusOK, gin.H{
		"code": 20000,
		"data": dataMap,
	})
	return
}
